﻿
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
 namespace hutil {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：ClassUtil                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2021/6/6 9:34:49                        
   *└────────────────────────────────────────────────┘
   **/
    public class ToStr {
        private static readonly ConcurrentDictionary<RuntimeTypeHandle ,string> TypeTableName = new ConcurrentDictionary<RuntimeTypeHandle ,string>();
        private static readonly ConcurrentDictionary<String ,string> strName = new ConcurrentDictionary<String ,string>();
        public static string GetTableName(Type type) {
            string name="";
            if( !TypeTableName.TryGetValue(type.TypeHandle ,out name) ) {
                name = type.Name;
                if( type.IsInterface && name.StartsWith("I") )
                    name = name.Substring(1);
                //NOTE: This as dynamic trick should be able to handle both our own Table-attribute as well as the one in EntityFramework 
                var tableattr = type.GetCustomAttributes(false).Where(attr => attr.GetType().Name == "TableAttribute").SingleOrDefault() as
                    dynamic;
                if( tableattr != null ) {
                    name = tableattr.Name;
                }
                name = camelCase2_(name);
                TypeTableName[type.TypeHandle] = name;
            }
            return name;
        }
        public static String camelCase2_(Type type) {
         
            return GetTableName(type);
        }
        public static String camelCase2_(String className) {
    
                    string name = "";
            if( !strName.TryGetValue(className ,out name) ) {


                string regularExpression = "[A-Z]";
                Regex rg = new Regex(regularExpression);
                StringBuilder tableName = new StringBuilder();
                for( int i = 0;i < className.Length;i++ ) {
                    if( rg.IsMatch(className[i].ToString()) && i != 0 ) {
                        tableName.Append("_");
                        tableName.Append(className[i].ToString());
                    } else {
                        tableName.Append(className[i].ToString());
                    }
                }
                name= tableName.ToString();
                strName[className] = name;
            }
            return name;
        }

        public static String formatInt(String str ,params object[] a) {
         
            for( int i = 0;i < a.Length;i++ ) {
                a[i] = (int)a[i];
            }
           return  String.Format(str ,a);


        }
    }
}
